<?php
if (! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );
class Authentication extends Public_Controller {
	
	private $b_Check = false;

	public function __construct(){
        parent::Public_Controller();
        $this->layout->wrapper('layouts/login');
		#Tải thư viện  và helper của Form trên CodeIgniter
		$this->load->library('form_validation');
		#Tải model
		$this->load->model(array('authentication_model_m'));
	}

	public function login(){

		$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'Password', 'required');
		
		#Kiểm tra điều kiện validate
		if($this->form_validation->run() == TRUE){
			$a_UserInfo['username'] = $this->input->post('username');
			$a_UserInfo['password'] = sha1($this->input->post('password'));
			$a_UserChecking = $this->authentication_model_m->get_where( $a_UserInfo );
			if($a_UserChecking){
				$this->session->set_userdata('user', $a_UserChecking);
                                //die(base_url('authentication/success'));
				//redirect(base_url('authentication/success'));
                                redirect('authentication/success');
			}else{
				$this->b_Check = false;
			}
		}
        $this->data->b_Check= $this->b_Check;
        $this->layout->create('authentication-template',$this->data);
		
	}

	public function logout(){
		$this->session->unset_userdata('user');	// Unset session of user
		redirect(base_url('authentication'));
	}
	
	public function success(){
		$a_UserInfo['user'] = $this->session->userdata('user');
		$this->load->view('success-template', $a_UserInfo);
	}

}

